﻿using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Text;

namespace ARS.Control.Attributes
{
    /// <summary>
    /// basicmessage_ruleralation:实体类(属性说明自动提取数据库字段的描述信息)
    /// </summary>
    [DataContract]
    [Serializable]
    public class DbSelectAll:Attribute
    {
        public DbSelectAll ()
        {
            KeyField = "Id";
            SelectFields = "*";
            //OrderField = KeyField;
            StrWhere = "1=1";
        }

        /// <summary>
        /// 表名
        /// </summary>
        [DataMember(IsRequired = false)]
        [Bindable(true), Category("Appearance"), Description("表名，如果是关键字 请用[]包含"), NotifyParentProperty(true)]
        public string TableName
        {
            get;
            set;
        }
        /// <summary>
        /// 查询字段
        /// </summary>
        [DataMember(IsRequired = false)]
        public string SelectFields
        {
            get;
            set;
        }


        private string _OrderField;
        /// <summary>
        /// 排序字段
        /// </summary>
        [DataMember(IsRequired = false)]
        public string OrderField
        {
            get
            {
                if (this._OrderField == null)
                {
                    this._OrderField = this.KeyField;
                }
                return _OrderField;
            }
            set
            {
                this._OrderField = value;
            }
        }
        /// <summary>
        /// 查询条件
        /// </summary>
        [DataMember(IsRequired = false)]
        public string StrWhere
        {
            get;
            set;
        }

        /// <summary>
        /// 主键
        /// </summary>
        [DataMember(IsRequired = false)]
        public string KeyField
        {
            get;
            set;
        }
        /// <summary>
        /// 获取查询语句
        /// </summary>
        /// <returns></returns>
        
        public string GetSelectAllSql()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("select ");
            if (string.IsNullOrEmpty(this.SelectFields))
            {
                sb.Append("*");
            }
            else
            {
                sb.Append(this.SelectFields);
            }
            sb.Append(" from ");
            sb.Append(this.TableName);
            if (!string.IsNullOrEmpty(this.StrWhere))
            {
                sb.Append(" where ");
                sb.Append(this.StrWhere);
            }
            if (!string.IsNullOrEmpty(this.OrderField))
            {
                sb.Append(" order by ");
                sb.Append(this.OrderField);
            }
            return sb.ToString();
        }

    }
}
